from gi.repository import Unity, Gio, GObject, Dbusmenu
import unity_pb2

def pushNotification (title, body, icon):
    n = pynotify.Notification (title, body, icon);
    n.set_hint_string ("x-canonical-append", "true");
    n.show ()
    time.sleep (3)

class Checkbox(Dbusmenu.Menuitem):
    def __init__(self, toggle_handler):
        super(Checkbox, self).__init__()
        self.toggle_handler = toggle_handler
        self.property_set ('toggle-type', 'checkmark')
        self.connect_object('item-activated', self.toggle_checkbox,self)
        
    def toggle_checkbox(self, second, third):
        selected = self.property_get_int('toggle-state')
        if selected:
            self.property_set_int('toggle-state', 0)
        else:
            self.property_set_int('toggle-state', 1)
        self.toggle_handler(not selected)
        
def checkbox_handler(item, num):
    selected = toggle_checkbox(item, num)
    # do the stuff that should be done, remember to use the 'selected' variable

# this function just toggles the state of checkbox
def toggle_checkbox(item, num):
    selected = item.property_get_int('toggle-state')
    if selected:
        item.property_set_int('toggle-state', 0)
    else:
        item.property_set_int('toggle-state', 1)
    return not selected
    # selected = item.property_get_int('toggle-state')
    # here write our code and use the 'selected' variable which indicates the
        
# your own handler
def your_handler(item, num):
    selected = toggle_checkbox(item, num)
    # here write our code and use the 'selected' variable which indicates the state of checkbox
        
def fake(object, sec, id):
    print "first: " + str(object) + " second: " + str(sec) + " third: " + str(id)

#the 'selected' argument will be of type 'bool' whether item is or is not selected
def handle(selected):
    print "checkbox is: " + str(selected)

if __name__ == '__main__':
    print int(False)
    print 'run\n'
    for attr in dir(unity_pb2): print attr
    um_launcher_entry = Unity.LauncherEntry.get_for_desktop_id ('kadu.desktop')
    quicklist = Dbusmenu.Menuitem.new()
    
    #checkbox = Dbusmenu.Menuitem.new()
    #checkbox.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "This is a checkbox")
    #checkbox.property_set ('toggle-type', 'checkmark')
    #checkbox.connect('item-activated', toggle_checkbox)
    
    # the only thing you must do is provide a one-argument function to the constructor
    # which will handle the state of the checkbox (of course remember to copy the 'Checkbox' class code too)
    checkbox = Checkbox(handle)
    checkbox.property_set ('label', "")
    quicklist.child_append(checkbox)
    
    item = Dbusmenu.Menuitem.new()
    item.property_set('type',Dbusmenu.CLIENT_TYPES_SEPARATOR)
    quicklist.child_append(item)
    
    radio = Dbusmenu.Menuitem.new()
    radio.property_set('label', "Finally a Radio Button")
    radio.property_set('toggle-type', 'radio')
    radio.property_set_int('toggle-state', 1)
    radio.connect('item-activated', toggle_checkbox)
    quicklist.child_append(radio)
    
    print radio.property_get_int('toggle-state')
    
    print "toggle type: " + str(checkbox.property_get(Dbusmenu.MENUITEM_PROP_TOGGLE_TYPE))
    print "toggle state: " + str(checkbox.property_get(Dbusmenu.MENUITEM_PROP_TOGGLE_STATE))
    print "type: " + str(item.property_get(Dbusmenu.MENUITEM_PROP_TYPE))
    print Dbusmenu.CLIENT_TYPES_SEPARATOR
    print Dbusmenu.CLIENT_TYPES_DEFAULT
    print Dbusmenu.CLIENT_TYPES_IMAGE
    print Dbusmenu.MENUITEM_ICON_NAME_BLANK
    for attr in dir(Dbusmenu): print attr
    
    
    
    
    update_menu_item = Dbusmenu.Menuitem.new()
    update_menu_item.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "Check for Updates")
    update_menu_item.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
    update_menu_item.connect ("item-activated", fake, 47)
    quicklist.child_append(update_menu_item)

    install_all_updates_menu_item = Dbusmenu.Menuitem.new()
    install_all_updates_menu_item.property_set (Dbusmenu.MENUITEM_PROP_LABEL, "Install All Available Updates")
    install_all_updates_menu_item.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
    #install_all_updates_menu_item.connect ("item-activated", self.install_all_updates, None)
    quicklist.child_append (install_all_updates_menu_item)

    um_launcher_entry.set_property ("quicklist", quicklist)
    GObject.MainLoop().run()
    
